#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
#include<string.h>
int mergeStones(int* stones, int stonesSize, int k) {
    int temp = stonesSize, judge=0;
    int sum = 0;
    int flag = 0;
    int sum1 = 0;
    while ((temp != 1) && (temp != 0))
    {
        judge = temp;
        if (temp % k == 0)
        {
            if (temp / k / k == 1)
                flag = 1;
        }
        temp /= k;
    }
    if (flag == 1)
    {    
        for (int i=0; i < stonesSize; i++)
        {
            sum1 += stones[i];
        }
        temp = stonesSize;
    }
    else
    {
        if (((judge + 1) % k )== 0)
        {
            int min = 500, mi = 0;
            int i;
            int sumtemp = 0;
            for (i = k - 1; i < stonesSize; i++)
            {
                if (i == k - 1)
                {
                    for (int j = 0; j <= k - 1; j++)
                    {
                        sumtemp += stones[j];
                    }
                }
                else
                {
                    sumtemp -= stones[i - k];
                    sumtemp += stones[i];
                }
                if (sumtemp < min)
                {
                    mi = i - k + 1;
                    min = sumtemp;
                }
            }
            sum += sumtemp;
            for (int i = 0; i < stones; i++)
            {
                if (i == mi)
                {
                    i += k - 1;
                }
                else
                {
                    sum1 += stones[i];
                }
            }
            sum1 += sumtemp;
            temp = stonesSize - k + 1;
        }
        else
            return -1;
    }
    while (temp != 1)
    {
        sum += sum1;
        temp /= k;
    }
    return sum;
}
int main()
{
    int arr[30] = { 0 };
    int n,k;
    scanf("%d %d", &n,&k);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    int ret = mergeStones(arr, n, k);
    printf("%d", ret);
    return 0;
}